#include <stdio.h>
#include <stdlib.h>

struct ListNode
{
    int val;
    struct ListNode *next;
};

struct ListNode *mergeLists(struct ListNode *list1, struct ListNode *list2);
void printList(struct ListNode *head);
struct ListNode *createList(int listSize);

int main()
{
    int listSize;
    scanf("%d", &listSize); // 读取单个链表的大小
    // printf("%d", listSize);
    struct ListNode *list1 = createList(listSize);

    scanf("%d", &listSize); // 读取单个链表的大小
    struct ListNode *list2 = createList(listSize);

    // 归并排序链表
    struct ListNode *result = mergeLists(list1, list2);

    // 输出排序后的链表
    printList(result);
}

struct ListNode *createList(int listSize)
{
    struct ListNode *head = NULL;
    struct ListNode *current = NULL;

    // 读取链表的所有节点值
    for (int j = 0; j < listSize; j++)
    {
        int num;
        scanf("%d", &num);

        struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode));
        node->val = num;
        node->next = NULL;

        if (head == NULL)
        {
            head = node;
            current = node;
        }
        else
        {
            current->next = node;
            current = node;
        }
    }
    return head;
}

void printList(struct ListNode *head)
{
    struct ListNode *current = head;
    while (current != NULL)
    {
        printf("%d ", current->val);
        current = current->next;
    }
    printf("\n");
}

struct ListNode *mergeLists(struct ListNode *list1, struct ListNode *list2) 
{
    // 永远先判空  0 <= N <= 100000
    if (!list1)
    {
        return list2;
    }
    if (!list2) 
    {
        return list1;
    }

    // 二叉树几天没写了
    // 先来一个哑节点
    struct ListNode*dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
    dummy->next = NULL;

    struct ListNode*t1 = list1;
    struct ListNode*t2 = list2;
    struct ListNode*to_link = dummy;

    while (t1&&t2)
    {
        if (t1->val <= t2->val)
        {
            // 偷懒大神这次不偷懒了吧
            // 先将to_link连接至t1
            to_link->next = t1;

            // t1后移
            t1 = t1->next;

            // to_link 后移
            to_link = to_link->next;

            // 将to_link重新滞空
            to_link->next = NULL;
        }
        else
        {
            // 先将to_link连接至t2
            to_link->next = t2;

            // t2后移
            t2 = t2->next;

            // to_link 后移
            to_link = to_link->next;

            // 将to_link重新滞空
            to_link->next = NULL;
        }
    }

    // 如果t1为空
    if (!t1)
    {
        to_link->next = t2;
    }

    if (!t2)
    {
        to_link->next = t1;
    }

    // 利用哑节点确实简化操作
    return dummy->next;
}